import { Post, Get, Put, Delete } from '@/utils/request.ts'

export interface paginationParamsType {
  pageNo: string | number
  pageSize: number | string
  name?: string
}

export interface CreateRoleDto {
  name: string
  code: string
}

export const createRolesApi = (data: CreateRoleDto) => {
  return Post<CreateRoleDto, void>('/blog/roles', data)
}

export const getRolesListApi = (data: paginationParamsType) => {
  return Get<paginationParamsType, { records: Record<string, string>[]; total: number }>(
    '/blog/roles',
    data,
  )
}

export const getRolesByIdApi = (id: string | number) => {
  return Get<never, Record<never, string>[]>('/blog/roles/' + id)
}

export const deleteRolesApi = (id: string | number) => {
  return Delete<never, void>('blog/roles/' + id)
}

export const updateRolesApi = (id: string | number, data: CreateRoleDto) => {
  return Put<CreateRoleDto, void>('blog/roles/' + id, data)
}

export interface getRoleUsersParamsType {
  page?: number
  limit?: number
  orderBy?: string
  order?: 'ASC' | 'DESC'
}
export interface getRoleUsersDataType {
  role: {
    id: number
    name: string
    code: string
  }
  users: {
    account: string
    username: string
    password: string
  }[]
  pagination: {
    total: number
    currentPage: number
    totalPages: number
    limit: number
  }
}

export const getRoleUsersApi = (id: string | number, data: getRoleUsersParamsType) => {
  return Get<getRoleUsersParamsType, getRoleUsersDataType>(`/blog/roles/${id}/users`, data)
}

export interface Role {
  id: number
  name: string
  code: string
  permissions: {
    id: number
    name: string
    type: string
    description: string
    code: string
  }[]
}
export const getRolePermissionApi = (id: string | number) => {
  return Get<never, Role>(`/blog/roles/${id}/permissions`)
}

export const setRolePermissionApi = (id: string | number, data: { permissionIds: number[] }) => {
  return Put<{ permissionIds: number[] }, never>(`/blog/roles/${id}/permissions`, data)
}

export const setRoleMenuApi = (roleId: string | number, data: { menuIds: string[] | number[] }) => {
  return Put<{ menuIds: string[] | number[] }, never>(`/blog/roles/${roleId}/menus`, data)
}
